import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/base/BaseRepository.dart';
import 'package:play_android/com/zhihaoliang/const/AppApi.dart';
import 'package:play_android/com/zhihaoliang/http/ResultData.dart';
import 'package:play_android/com/zhihaoliang/http/HttpRequest.dart';
import 'package:play_android/com/zhihaoliang/play/home/model/banner/BannerItem.dart';
import 'package:play_android/com/zhihaoliang/play/home/model/banner/BannerModel.dart';

///  ***************************************************************************
///  "主页轮训图片"请求处理
///  ***************************************************************************
typedef ProgressCallback = void Function(int count, int total);

class BannerPresenter extends BaseRepository<List<BannerItem>> {
  BannerCallBack _callBack;

  BannerPresenter([this._callBack]);

  load() {
    fetch()
        .then((list) => _callBack.onLoadBannerComplete(list))
        .catchError((onError) => _callBack.onLoadBannerError(onError));
  }

  @override
  List<BannerItem> fetchModel(Map<String, dynamic> map) {
    return BannerModel.fromJson(map).data;
  }

  @override
  Future<ResultData> fetchResultData(
      [String url, Map<String, dynamic> map]) async {
    return await HttpRequest().get(AppApi.BANNER);
  }
}

abstract class BannerCallBack {
  void onLoadBannerComplete(List<BannerItem> list);

  void onLoadBannerError(HttpException httpException);
}
